package Calculator.Stack;
public class Linklist implements List{
    private node top;
    private int  length;
    
    public Linklist(){
        this.top=null;
        this.length=0;
    }
    public boolean isEmpty(){
        return this.top==null;
    }
    public void push(Object o){
        node a=new node(o);
        if(this.top==null){
            a.next=this.top;
            this.top=a;
            this.length++;
        }
        else{
            node pos=this.top;
            for(int i=1;i<this.length;i++)
                pos=pos.next;
            a.next=pos.next;
            pos.next=a;
            this.length++;
        }
        
    }
    public Object pop(){
        Object o=0;
        if(this.top==null)
            throw new ArrayIndexOutOfBoundsException("栈已经为空");
        node pos=this.top;
        for(int i=1;i<this.length-1;i++)
            pos=pos.next;
        if(!(pos.next==null)){
        o=pos.next.data;
        pos.next=pos.next.next;
        }
        else {
            o=pos.data;
            this.top=null;
        }
        this.length--;
        return o;
    }
    public Object getTop(){
        if(this.top==null)
            throw new ArrayIndexOutOfBoundsException("栈已经为空");
        node pos=this.top;
        for(int i=1;i<this.length;i++)
            pos=pos.next;
        return pos.data;
    }
}
